package com.zmn.uac.api.controllers.user;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.uac.api.constant.HeaderParamKeyConst;
import com.zmn.uac.client.UserModifyClient;
import com.zmn.uac.common.info.user.AvatarSrcOrNickName;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 类描述：用户信息
 *
 * @author xujie
 * @since 2021/06/17 10:49
 */
@Api(tags = "用户信息")
@Slf4j
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    private static final String TAG = "用户信息";

    @Resource
    UserModifyClient userModifyClient;

    /**
     * 修改头像或昵称
     *
     * @param avatarSrcOrNickName avatarSrcOrNickName
     * @return ResponseDTO<UserAuthLoginDRO>
     * @author xujie
     * @since 2021/06/14
     */
    @ApiOperation(value = "修改头像或昵称")
    @PostMapping(value = "/modifyAvatarSrcOrNickName",produces = MediaType.APPLICATION_JSON_VALUE, headers = {HeaderParamKeyConst.TOKEN_KEY})
    public ResponseDTO<String> modifyAvatarSrcOrNickName(HttpServletRequest request,
                                                         @RequestBody @Valid
                                                         @NotNull(message = "用户手机号验证码信息不能为null") AvatarSrcOrNickName avatarSrcOrNickName) {

        Long userId = (Long) request.getAttribute(HeaderParamKeyConst.USER_ID);
        if (StringUtil.isBlank(avatarSrcOrNickName.getAvatarSrc()) || StringUtil.isBlank(avatarSrcOrNickName.getNickName())) {
            return ResponseDTO.fail("修改头像和昵称不能都为null");
        }
        String result = "";
        if (StringUtils.isNotBlank(avatarSrcOrNickName.getNickName())) {
            Boolean modifyNickNameStatus = userModifyClient.modifyNickNameByUserId(userId, avatarSrcOrNickName.getNickName());
            if (!modifyNickNameStatus) {
                result += "修改昵称";
            }
        }
        if (StringUtils.isNotBlank(avatarSrcOrNickName.getAvatarSrc())) {
            Boolean modifyAvatarStatus = userModifyClient.modifyAvatarSrcByUserId(userId, avatarSrcOrNickName.getAvatarSrc());
            if (!modifyAvatarStatus) {
                if (StringUtil.isNotBlank(result)) {
                    result += "、";
                }
                result += "修改头像";
            }
        }
        if (StringUtil.isNotBlank(result)) {
            result += "失败";
            return ResponseDTO.fail(result);
        } else {
            return ResponseDTO.success("修改成功");
        }
    }
}
